import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:provider/provider.dart';
import 'package:ytbusiness/config/yt_theme.dart';
import 'package:ytbusiness/page/drawer/scout_drawer_content/scout_login/scout_login_view_model.dart';
import 'package:ytbusiness/state/provider_widget.dart';
import 'package:ytbusiness/utils/helper.dart';
import 'package:ytbusiness/utils/screen_adaptation.dart';
import 'package:ytbusiness/widgets/widgets.dart';

///登录云途养护勘察员
class ScoutLogin extends StatefulWidget {
  @override
  _ScoutLoginState createState() => _ScoutLoginState();
}

class _ScoutLoginState extends State<ScoutLogin> {
  int _select = 0; //选中单位
  TextEditingController unit = TextEditingController(); //单位
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        //输入框失焦
        FocusScope.of(context).requestFocus(FocusNode());
      },
      child: Scaffold(
        backgroundColor: Colors.white,
        appBar: customAppBar(context, title: ""),
        body: SingleChildScrollView(
          child: ProviderWidget<ScoutLoginViewModel>(
            builder: (context, model, child) {
              return Column(
                mainAxisAlignment: MainAxisAlignment.start,
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  SizedBox(
                    height: setWidth(70),
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      SizedBox(
                        width: setWidth(20),
                      ),
                      Text(
                        '登录云途养护',
                        style: TextStyle(
                          color: Color(0xff333333),
                          fontSize: setSp(26),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      Text(
                        "勘察员",
                        style: TextStyle(
                          color: YTTheme.theme,
                          fontSize: setSp(26),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ],
                  ),
                  SizedBox(
                    height: setWidth(76),
                  ),
                  _editBox(
                      title: "登录邮箱", hint: "请输入邮箱", controller: model.email),
                  _unitBox(title: "所属单位", hint: "请选择所属单位", controller: unit),
                  _editBox(
                      title: "登录密码", hint: "请输入密码", controller: model.passWord),
                  SizedBox(
                    height: setWidth(100),
                  ),
                  GestureDetector(
                    onTap: () async {

                      urlPush(context, "yuntu://scout");
                      // await model.login(context, {
                      //   "email": model.email.text.toString().trim(),
                      //   "cate": "1",
                      //   "password": model.passWord.text.toString().trim()
                      // });
                    },
                    child: Container(
                      child: Container(
                        height: setWidth(45),
                        alignment: Alignment.center,
                        margin: EdgeInsets.only(
                            left: setWidth(17), right: setWidth(17)),
                        decoration: BoxDecoration(
                            color: YTTheme.theme,
                            borderRadius: BorderRadius.circular(5)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          children: [
                            Text(
                              "立即登录",
                              style: TextStyle(
                                  color: Colors.white, fontSize: setSp(15)),
                            ),
                          ],
                        ),
                      ),
                    ),
                  )
                ],
              );
            },
            model: ScoutLoginViewModel(Provider.of(context)),
          ),
        ),
      ),
    );
  }

  //编辑item
  Container _editBox(
      {String title, String hint, TextEditingController controller}) {
    return Container(
      height: setWidth(40),
      margin: EdgeInsets.only(
          left: setWidth(20), right: setWidth(20), bottom: setWidth(17)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            child: Text(
              title,
              style: TextStyle(color: Color(0xff323232), fontSize: setSp(15)),
            ),
          ),
          SizedBox(
            width: setWidth(15),
          ),
          Expanded(
              child: Container(
            padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(1),
                border:
                    Border.all(color: Color(0xffF0F0F0), width: setWidth(1))),
            height: setWidth(40),
            alignment: Alignment.centerLeft,
            child: TextField(
              controller: controller,
              textAlign: TextAlign.start,
              maxLines: 1,
              keyboardType: TextInputType.multiline,
              autofocus: false,
              decoration: InputDecoration(
                border: InputBorder.none,
                focusedBorder: InputBorder.none,
                enabledBorder: InputBorder.none,
                hintText: hint,
                hintStyle:
                    TextStyle(color: Color(0xff999999), fontSize: setSp(14)),
              ),
            ),
          )),
        ],
      ),
    );
  }

  //所属单位item
  Container _unitBox(
      {String title, String hint, TextEditingController controller}) {
    return Container(
      height: setWidth(40),
      margin: EdgeInsets.only(
          left: setWidth(20), right: setWidth(20), bottom: setWidth(17)),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            child: Text(
              title,
              style: TextStyle(color: Color(0xff323232), fontSize: setSp(15)),
            ),
          ),
          SizedBox(
            width: setWidth(15),
          ),
          Expanded(
              child: Container(
            padding: EdgeInsets.only(left: setWidth(15), right: setWidth(15)),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(1),
                border:
                    Border.all(color: Color(0xffF0F0F0), width: setWidth(1))),
            height: setWidth(40),
            alignment: Alignment.centerLeft,
            child: Row(
              children: [
                Expanded(
                  child: TextField(
                    readOnly: true,
                    controller: controller,
                    textAlign: TextAlign.start,
                    maxLines: 1,
                    keyboardType: TextInputType.multiline,
                    autofocus: false,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      focusedBorder: InputBorder.none,
                      enabledBorder: InputBorder.none,
                      hintText: hint,
                      hintStyle: TextStyle(
                          color: Color(0xff999999), fontSize: setSp(14)),
                    ),
                    onTap: () {
                      _selectUnit();
                    },
                  ),
                ),
                SizedBox(
                  width: setWidth(10),
                ),
                GestureDetector(
                  behavior: HitTestBehavior.opaque,
                  onTap: () {
                    _selectUnit();
                  },
                  child: Container(
                    padding: EdgeInsets.only(
                        left: setWidth(3),
                        right: setWidth(3),
                        top: setWidth(2),
                        bottom: setWidth(2)),
                    width: setWidth(21),
                    height: setWidth(12),
                    child: Image.asset(
                      "assets/images/selete_unit.png",
                      fit: BoxFit.cover,
                    ),
                  ),
                ),
              ],
            ),
          )),
        ],
      ),
    );
  }

  ///选择单位
  void _selectUnit() {
    showModalBottomSheet(
        backgroundColor: Colors.white,
        context: context,
        shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(10),
            topRight: Radius.circular(10),
          ),
        ),
        builder: (BuildContext context) {
          return SizedBox(
            height: setWidth(369),
            child: Column(
              children: [
                Container(
                  margin:
                      EdgeInsets.only(top: setWidth(17), bottom: setWidth(17)),
                  child: Text(
                    "请选择所在单位",
                    style: TextStyle(
                        color: Color(0xff333333), fontSize: setSp(18)),
                  ),
                ),
                Container(
                  height: setWidth(0.5),
                  color: Color(0xffEEEEEE),
                ),
                Expanded(
                  child: ListView.builder(
                      shrinkWrap: true,
                      itemCount: 10,
                      itemBuilder: (context, index) {
                        return GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          onTap: () {
                            setState(() {
                              unit.text = "合肥总公司";
                              _select = index;
                            });
                            Navigator.pop(context);
                          },
                          child: Column(
                            children: [
                              Container(
                                margin: EdgeInsets.only(
                                    left: setWidth(25), right: setWidth(25)),
                                padding: EdgeInsets.only(
                                    top: setWidth(20), bottom: setWidth(20)),
                                decoration: BoxDecoration(
                                    border: Border(
                                        bottom: BorderSide(
                                            color: Color(0xffF0F0F0),
                                            width: setWidth(0.5)))),
                                child: Row(
                                  mainAxisAlignment:
                                      MainAxisAlignment.spaceBetween,
                                  children: [
                                    Text(
                                      "合肥总公司",
                                      style: TextStyle(
                                          color: Color(0xff333333),
                                          fontSize: setSp(16)),
                                    ),
                                    _select == index
                                        ? Container(
                                            child: Image.asset(
                                              "assets/images/check_pay.png",
                                              width: setWidth(17),
                                              height: setWidth(12),
                                            ),
                                          )
                                        : Container()
                                  ],
                                ),
                              ),
                            ],
                          ),
                        );
                      }),
                )
              ],
            ),
          );
        });
  }
}
